﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using MyLibrary.ThreeLayer.DataAccess.Interface;

namespace MyLibrary.ThreeLayer.DataAccess
{
    public abstract class Transaction : ITransaction
    {
        protected IDbConnection _innerConnection = null;
        protected IDbTransaction _innerTransaction = null;

        protected Transaction()
        {
        }

        public void BeginTransaction()
        {
            if (_innerConnection == null)
                throw new ArgumentNullException("连接不能为空");
            this._innerTransaction = this._innerConnection.BeginTransaction();
        }

        public void BeginTransaction(IsolationLevel isolationLevel)
        {
            if (_innerConnection == null)
                throw new ArgumentNullException("连接不能为空");
            this._innerTransaction = this._innerConnection.BeginTransaction(isolationLevel);
        }

        public void Commit()
        {
            if (this._innerTransaction == null)
                throw new ArgumentNullException("事务未开始");
            this._innerTransaction.Commit();
        }

        public void Rollback()
        {
            if (this._innerTransaction == null)
                throw new ArgumentNullException("事务未开始");
            this._innerTransaction.Rollback();
        }

        public IDbTransaction GetTransaction()
        {
            return this._innerTransaction;
        }
    }
}
